package com.css.zfzx.fxpg.modules.percent.damage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.fxpg.common.utils.BeanUtil;
import com.css.zfzx.fxpg.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.fxpg.modules.percent.damage.repository.FxDamagePercentNativeRepository;
import com.css.zfzx.fxpg.modules.percent.damage.repository.FxDamagePercentRepository;
import com.css.zfzx.fxpg.modules.percent.damage.repository.entity.FxDamagePercentEntity;
import com.css.zfzx.fxpg.modules.percent.damage.service.FxDamagePercentService;
import com.css.zfzx.fxpg.modules.percent.damage.viewobjects.FxDamagePercentQueryParams;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.fxpg.modules.percent.damage.viewobjects.FxDamagePercentVO;
import com.css.zfzx.fxpg.modules.percent.daynight.repository.entity.FxDayNightPercentEntity;
import com.css.zfzx.fxpg.modules.percent.daynight.viewobjects.FxDayNightPercentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-13
 */
@Service
public class FxDamagePercentServiceImpl implements FxDamagePercentService {

    @Autowired
    private FxDamagePercentRepository fxDamagePercentRepository;
    @Autowired
    private FxDamagePercentNativeRepository fxDamagePercentNativeRepository;

    @Override
    public JSONObject queryFxDamagePercents(FxDamagePercentQueryParams queryParams, int curPage, int pageSize) {
        Page<FxDamagePercentEntity> fxDamagePercentPage = this.fxDamagePercentNativeRepository.queryFxDamagePercents(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(fxDamagePercentPage);
        return jsonObject;
    }


    @Override
    public FxDamagePercentEntity getFxDamagePercent(String id) {
        FxDamagePercentEntity fxDamagePercent = this.fxDamagePercentRepository.findById(id).orElse(null);
        return fxDamagePercent;
    }

    @Override
    public boolean isRepeat(String structureType) {
        FxDamagePercentEntity damagePercentEntity = fxDamagePercentRepository.findDistinctByStructureType(structureType);
        if (damagePercentEntity != null) {
            return true;
        }
        return false;
    }

    @Override
    public void coverFxDamagePercent(FxDamagePercentEntity fxDamagePercent) {
        FxDamagePercentEntity old = fxDamagePercentRepository.findDistinctByStructureType(fxDamagePercent.getStructureType());
        BeanUtil.copyPropertiesIgnoreEmpty(fxDamagePercent, old);
        this.updateFxDamagePercent(old);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxDamagePercentEntity saveFxDamagePercent(FxDamagePercentEntity fxDamagePercent) {
        String uuid = UUIDGenerator.getUUID();
        fxDamagePercent.setId(uuid);
        fxDamagePercent.setCreateUser(PlatformSessionUtils.getUserId());
        fxDamagePercent.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.fxDamagePercentRepository.save(fxDamagePercent);
        return fxDamagePercent;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FxDamagePercentEntity updateFxDamagePercent(FxDamagePercentEntity fxDamagePercent) {
        fxDamagePercent.setUpdateUser(PlatformSessionUtils.getUserId());
        fxDamagePercent.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = fxDamagePercent.getId();
        FxDamagePercentEntity one = fxDamagePercentRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(fxDamagePercent, one);
        this.fxDamagePercentRepository.save(one);
        return fxDamagePercent;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFxDamagePercents(List<String> ids) {
        List<FxDamagePercentEntity> fxDamagePercentList = this.fxDamagePercentRepository.findAllById(ids);
        if (!PlatformObjectUtils.isEmpty(fxDamagePercentList) && fxDamagePercentList.size() > 0) {
            for (FxDamagePercentEntity fxDamagePercent : fxDamagePercentList) {
                this.fxDamagePercentRepository.delete(fxDamagePercent);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public String exportExcel(FxDamagePercentQueryParams queryParams, HttpServletResponse response) {
        try {
            List<FxDamagePercentEntity> entities = fxDamagePercentNativeRepository.listFxDamagePercents(queryParams);
            // 深拷贝
            ArrayList<FxDamagePercentVO> vos = new ArrayList<>();
            for (FxDamagePercentEntity entity : entities) {
                FxDamagePercentVO vo = new FxDamagePercentVO();
                BeanUtils.copyProperties(entity, vo);
                vos.add(vo);
            }
            // 导出
            EasyPoiExcelUtil.exportExcel(vos, "建筑破坏损失比", "建筑破坏损失比", FxDamagePercentVO.class, "建筑破坏损失比.xlsx", response);
            return "导出成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Override
    public List<FxDamagePercentEntity> getFxDamagePercents(FxDamagePercentQueryParams queryParams) {
        List<FxDamagePercentEntity> fxDamagePercents = this.fxDamagePercentNativeRepository.listFxDamagePercents(queryParams);
        return fxDamagePercents;
    }

}
